package cn.wen.yaling.controller;

import java.util.List;
import java.util.Map;

import cn.wen.yaling.aop.annotation.operation.OperationLogger;
import cn.wen.yaling.commons.vo.*;
import cn.wen.yaling.xo.service.CommentService;
import cn.wen.yaling.xo.service.WebConfigService;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import cn.wen.yaling.commons.entity.UserEntity;
import cn.wen.yaling.xo.service.UserService;
import cn.wen.yaling.commons.utils.PageUtils;
import cn.wen.yaling.commons.utils.ResultEntity;

/**
 * 
 *
 * @author FY
 * @email 932043654@qq.com
 * @date 2022-08-13 02:07:37
 */
@RestController
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private WebConfigService webConfigService;

    /**
     * 获取关于我页面的全部数据
     * @return
     */
    @GetMapping("/web/about/comment-list")
    public ResultEntity getAboutCommentList() {
        List<CommentVO> comments = commentService.getAboutCommentList();
        return ResultEntity.ok().put("data", comments);
    }

    /**
     * 获取系统的数据
     * @return
     */
    @GetMapping("/web/config-info")
    public ResultEntity getConfigInfo() {
        ConfigVO configInfoIsActive = webConfigService.getConfigInfoIsActive();
        return ResultEntity.ok().put("data", configInfoIsActive);
    }

    /**
     * 获取系统的数据
     * @return
     */
    @PostMapping("/web/config/add-config-views")
    public ResultEntity addConfigViews() {
        // 存储到到mysql中
        webConfigService.addConfigViews();
        return ResultEntity.ok();
    }

    @GetMapping("/admin/user-info")
    public ResultEntity getUserInfo(@RequestParam("userId") String userId) {
        return userService.getUserInfoByUserId(userId);
    }


    @ApiOperation(value = "获取用户列表", notes = "获取用户列表")
    @GetMapping("/admin/user/list")
    public ResultEntity getUserList() {
        return userService.getUserList();
    }

    @ApiOperation(value = "查询用户列表", notes = "查询用户列表")
    @GetMapping("/admin/user/search/list")
    public ResultEntity getUserList(@RequestParam("keyword") String keyword) {
        return userService.searchUserList(keyword);
    }

    @OperationLogger(value = "添加用户")
    @ApiOperation(value = "添加用户", notes = "添加用户", response = ResultEntity.class)
    @PostMapping("/admin/user/add")
    public ResultEntity add(@RequestBody UserAddVO userVO) {
        return userService.addUser(userVO);
    }

    @OperationLogger(value = "分配角色")
    @ApiOperation(value = "分配角色", notes = "分配角色", response = ResultEntity.class)
    @PostMapping("/admin/user/role/update")
    public ResultEntity updateUserRoleRelation(@RequestBody UserRoleUpdateVO userVO) {
        return userService.updateUserRoleRelation(userVO);
    }


    @OperationLogger(value = "编辑用户")
    @ApiOperation(value = "编辑用户", notes = "编辑用户", response = ResultEntity.class)
    @PostMapping("/admin/user/update")
    public ResultEntity edit(@RequestBody UserUpdateVO userVO) {
        return userService.editUser(userVO);
    }

    @OperationLogger(value = "删除用户")
    @ApiOperation(value = "删除用户", notes = "删除用户", response = ResultEntity.class)
    @PostMapping("/admin/user/delete")
    public ResultEntity deleteUser(@RequestParam("userIds") List<Integer> userIds) {
        return userService.deleteBatchUserByIds(userIds);
    }

    @OperationLogger(value = "重置用户密码")
    @ApiOperation(value = "重置用户密码", notes = "重置用户密码", response = ResultEntity.class)
    @PostMapping("/admin/user/reset-password")
    public ResultEntity resetUserPassword(@RequestParam("userId") String userId) {
        return userService.resetUserPassword(userId);
    }

    @ApiOperation(value = "获取用户角色的所有菜单权限", notes = "获取用户角色的所有菜单权限", response = ResultEntity.class)
    @GetMapping("/admin/user/menu")
    public ResultEntity getUserMenuList(@RequestParam("userId") String userId) {
        return userService.getUserMenuList(userId);
    }

}
